<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/27
 * Time: 18:14
 */

namespace app\index\model;
use think\Model;
use think\Db;
class ApplyModel extends Model{
    // 确定链接表名
    protected $table = 'snake_apply';
    public function getLists($param){
        $pageNo=1;$pageSize=10;
        if(isset($param['pageNo']) && $param['pageNo']!='' && $param['pageNo']>1) $pageNo=intval($param['pageNo']);
        if(isset($param['pageSize']) && $param['pageSize']!='' && $param['pageSize']>0) $pageSize=intval($param['pageSize']);
        return $this->order('addtime DESC,order_id DESC')->limit(($pageNo-1)*$pageSize,$pageSize)->select();
    }
    public function getDrive($param){
        $pageNo=1;$pageSize=10;
        if(isset($param['pageNo']) && $param['pageNo']!='' && $param['pageNo']>1) $pageNo=intval($param['pageNo']);
        if(isset($param['pageSize']) && $param['pageSize']!='' && $param['pageSize']>0) $pageSize=intval($param['pageSize']);
        $user_id=session('user_id');
        $where=array(
            'first_leader'=>$user_id
        );
        if(paramValid($param,'type')){
            switch($param['type']){
                case 1:
                    $where['type']=1;
                    break;
                case 2:
                    $where['type']=0;
                    break;
            }
        }
        if(paramValid($param,'timer')){
            switch($param['timer']){
                case 1:
                    $where["FROM_UNIXTIME(addtime,'%Y-%m-%d')"]=date('Y-m-d');
                    break;
                case 2:
                    $where["FROM_UNIXTIME(addtime,'%Y-%m')"]=date('Y-m');
                    break;
                case 3:
                    $twoMonthAgo=strtotime("-2 month", time());
                    $where["addtime"]=array('>=',$twoMonthAgo);
                    break;
            }
        }
        $ret= $this->where($where)->order('addtime DESC,order_id DESC')->limit(($pageNo-1)*$pageSize,$pageSize)->select();
        foreach($ret as $k=>$vo){
            $ret[$k]['addtime']=format_date($vo['addtime']);
        }
        return $ret;
    }
    public function getOne($id){
        return $this->where(array('order_id'=>$id))->find();
    }
    public function getLink($param){
        if($param['type']==1){
            $model=new LoanModel();
        }else{
            $model=new CardModel();
        }
        return $model->getLink($param['pid']);
    }
    public function add($param){
        $user_id=session('user_id');
        if($param['type']!=0 && $param['type']!=1) return msg(-1,'','参数错误');
        $smsLogModel=new SmsLogModel();
        $smsLog=$smsLogModel->findSmsLog($param);
        if($param['code'] != $smsLog['code']) return msg(-1, '', '验证码错误');
        if($param['type']==0){
            $model=new CardModel();
        }else{
            $model=new LoanModel();
        }
        $product=$model->getOne($param['pid']);
        if(empty($product)) return msg(-1,'','产品参数错误');
        $userModel=new UsersModel();
        $user=$userModel->getOne($user_id);
        $map=array(
            'pid'=>$param['pid'],
            'name'=>$product['name'],
            'thumb'=>$product['thumb'],
            'user_id'=>$user_id,
            'nickname'=>$user['nickname'],
            'head_pic'=>$user['head_pic'],
            'level'=>$user['level'],
            'first_leader'=>$user['first_leader'],
            'second_leader'=>$user['second_leader'],
            'third_leader'=>$user['third_leader'],
            'mobile'=>$param['mobile'],
            'realname'=>$param['realname'],
            'card_num'=>$param['card_num'],
            'type'=>$param['type'],
            'addtime'=>time()
        );
        try{
            $num=$this->save($map);
            if(false === $num){
                return msg(-1, '', '提交失败');
            }
            return msg(1,$product['link'],'提交成功');
        }catch(\Exception $e){
            return msg(-1, '', $e->getMessage());
        }
    }
    public function getCardProgress(){
        $user_id=session('user_id');
        $sql=Db::name('apply')->distinct(true)->field('pid')
            ->where(array('user_id'=>$user_id,'type'=>0))
            ->order('addtime DESC,order_id DESC')
            ->buildSql();
        return Db::table($sql)->alias('apply')->field('card.*')
            ->join('__CARD__ card','card.id=apply.pid')
            ->select();
    }
}